package com.hshx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hshx.pojo.Attendance;
import com.hshx.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RequestMapping("/workService")
@RestController
public class AttendanceController {
    @Autowired
    private AttendanceService attendanceService;

    /**
     * 新增一条考勤信息
     * @param attendance
     * @return
     */
    @RequestMapping("/addAttendance")
    public boolean addAttendance(@SpringQueryMap Attendance attendance){
        return attendanceService.save(attendance);
    }

    /**
     * 从数据库中移除一条考勤记录
     * @param attendanceId
     * @return
     */
    @RequestMapping("/removeAttendance")
    public boolean removeAttendance(int attendanceId){
        return attendanceService.removeById(attendanceId);
    }

    /**
     * 上班打卡
     * @return
     */
    @RequestMapping("/saveAttendance")
    public Map<String,Object> saveAttendance(int users_id, String date){
        System.out.println("date:"+date);

        Map<String, Object> map = new HashMap<>();
        String firstDate=date;
        String dataString=date.substring(0,10).trim();
        String head=dataString.substring(0,date.indexOf("-"));
        String foot=dataString.substring(date.indexOf("-")+1);
        System.out.println("foot:"+foot);

        if(dataString.length()==9){
            date=head+"-0"+foot;
        }else{
            date=dataString;
        }
        if(attendanceService.count(new QueryWrapper<Attendance>().eq("attendance_users",users_id).like("attendance_today",date))>0){
            map.put("status", false);
            map.put("msg","您今日已经完成上班打卡了,无需再次打卡!");
            return map;
        }
        Attendance attendance=new Attendance();
        attendance.setAttendance_users(users_id);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            attendance.setAttendance_work(sdf.parse(firstDate));
        } catch (ParseException e) {
//            throw new RuntimeException(e);
            map.put("status", false);
            map.put("msg","打卡失败,未知错误,请联系管理员!");
            return map;
        }
        if(attendanceService.save(attendance)){
            map.put("status", true);
            map.put("msg","上班打卡成功");
            return map;
        }else{
            map.put("status", false);
            map.put("msg","打卡失败,未知错误,请联系管理员!");
            return map;
        }
    }

    /**
     * 下班打卡
     * @return
     */
    @RequestMapping("/updateAttendance")
    public Map<String,Object> updateAttendance(int users_id,String date){
        Map<String, Object> map = new HashMap<>();
        String firstDate=date;
        String dataString=date.substring(0,10).trim();
        String head=dataString.substring(0,date.indexOf("-"));
        String foot=dataString.substring(date.indexOf("-")+1);
        if(dataString.length()==9){
            date=head+"-0"+foot;
        }else{
            date=dataString;
        }
        if(attendanceService.count(new QueryWrapper<Attendance>().eq("attendance_users",users_id).like("attendance_today",date))==0){
            map.put("status", false);
            map.put("msg","您今日还未进行过上班打卡,无法进行该下班打卡操作!");
            return map;
        }
        Attendance attendanceMysql=attendanceService.getOne(new QueryWrapper<Attendance>().eq("attendance_users",users_id).like("attendance_today",date));
        if(attendanceMysql.getAttendance_rest()!=null){
            map.put("status", false);
            map.put("msg","您今日已经进行过下班打卡,无需再次打卡!");
            return map;
        }

        Attendance attendance=new Attendance();

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            attendance.setAttendance_rest(sdf.parse(firstDate));
        } catch (ParseException e) {
//            throw new RuntimeException(e);
            map.put("status", false);
            map.put("msg","打卡失败,未知错误,请联系管理员!");
            return map;
        }
        if(attendanceService.update(attendance,new QueryWrapper<Attendance>().eq("attendance_users",users_id)
                .eq("attendance_today",sdf.format(attendanceMysql.getAttendance_today())))){
            map.put("status", true);
            map.put("msg","下班打卡成功!幸苦了!");
            return map;
        }else{
            map.put("status", false);
            map.put("msg","打卡失败,未知错误,请联系管理员!");
            return map;
        }
    }

    /**
     * 加班开始
     * @return
     */
    @RequestMapping("/updateStartAttendance")
    public Map<String,Object> updateStartAttendance(int users_id,String date){
        Map<String, Object> map = new HashMap<>();
        String firstDate=date;
        String dataString=date.substring(0,10).trim();
        String head=dataString.substring(0,date.indexOf("-"));
        String foot=dataString.substring(date.indexOf("-")+1);
        if(dataString.length()==9){
            date=head+"-0"+foot;
        }else{
            date=dataString;
        }
        if(attendanceService.count(new QueryWrapper<Attendance>().eq("attendance_users",users_id).like("attendance_today",date))==0){
            map.put("status", false);
            map.put("msg","您今日还未进行过上班打卡,无法进行该加班打卡操作!");
            return map;
        }
        Attendance attendanceMysql=attendanceService.getOne(new QueryWrapper<Attendance>().eq("attendance_users",users_id).like("attendance_today",date));
        if(attendanceMysql.getAttendance_start()!=null){
            map.put("status", false);
            map.put("msg","您今日已经进行过加班打卡,无需再次打卡!");
            return map;
        }

        Attendance attendance=new Attendance();
        Date mysqlTime=attendanceMysql.getAttendance_today();

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");
        try {
            attendance.setAttendance_start(sdf.parse(firstDate));
        } catch (ParseException e) {
//            throw new RuntimeException(e);
            map.put("status", false);
            map.put("msg","打卡失败,未知错误,请联系管理员!");
            return map;
        }
        if(attendanceService.update(attendance,new QueryWrapper<Attendance>().eq("attendance_users",users_id)
                .eq("attendance_today",sdf.format(mysqlTime)))){
            map.put("status", true);
            map.put("msg","加班打卡成功!");
            return map;
        }else{
            map.put("status", false);
            map.put("msg","打卡失败,未知错误,请联系管理员!");
            return map;
        }
    }
    /**
     * 加班结束
     * @return
     */
    @RequestMapping("/updateEndAttendance")
    public Map<String,Object> updateEndAttendance(int users_id,String date){
        Map<String, Object> map = new HashMap<>();
        String firstDate=date;
        String dataString=date.substring(0,10).trim();
        String head=dataString.substring(0,date.indexOf("-"));
        String foot=dataString.substring(date.indexOf("-")+1);
        if(dataString.length()==9){
            date=head+"-0"+foot;
        }else{
            date=dataString;
        }
        if(attendanceService.count(new QueryWrapper<Attendance>().eq("attendance_users",users_id).like("attendance_today",date))==0){
            map.put("status", false);
            map.put("msg","您今日还未进行过上班打卡,无法进行该加班结束打卡操作!");
            return map;
        }
        Attendance attendanceMysql=attendanceService.getOne(new QueryWrapper<Attendance>().eq("attendance_users",users_id).like("attendance_today",date));
        if(attendanceMysql.getAttendance_end()!=null){
            map.put("status", false);
            map.put("msg","您今日已进行过加班打卡,无需再次操作!");
            return map;
        }

        Attendance attendance=new Attendance();

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            attendance.setAttendance_end(sdf.parse(firstDate));
        } catch (ParseException e) {
//            throw new RuntimeException(e);
            map.put("status", false);
            map.put("msg","打卡失败,未知错误,请联系管理员!");
            return map;
        }
        if(attendanceService.update(attendance,new QueryWrapper<Attendance>().eq("attendance_users",users_id)
                .eq("attendance_today",sdf.format(attendanceMysql.getAttendance_today())))){
            map.put("status", true);
            map.put("msg","打卡成功!感谢您的努力工作!");
            return map;
        }else{
            map.put("status", false);
            map.put("msg","打卡失败,未知错误,请联系管理员!");
            return map;
        }
    }


    @RequestMapping("/overtime_end")
    public Map<String,Object> overtime_end(int users_id,String today) throws ParseException {
        Map<String, Object> map = new HashMap<>();
        String firstDate=today;
        String dataString=today.substring(0,10).trim();
        String head=dataString.substring(0,today.indexOf("-"));
        String foot=dataString.substring(today.indexOf("-")+1);
        if(dataString.length()==9){
            today=head+"-0"+foot;
        }else{
            today=dataString;
        }
        Attendance attendance=attendanceService.getOne(new QueryWrapper<Attendance>().eq("attendance_users",users_id).like("attendance_today",today));
        if(attendance==null||attendance.getAttendance_id()==null){
            map.put("status", false);
            map.put("msg","您今日还未进行过加班打卡,无法进行该操作!");
            return map;
        }
        Date now = new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time=attendance.getAttendance_start();
        Duration dur= Duration.between(time.toInstant() , now.toInstant());
        long between =dur.toMinutes();
        if(between<30){
            map.put("status", false);
            map.put("msg","加班时间必须大于30分钟!");
            return map;
        }else{
            map.put("status", true);
            return map;
        }
    }

    @RequestMapping("/getAttendanceCount")
    public long getAttendanceCount(int users_id,String date){
        String firstDate=date;
        String dataString=date.substring(0,10).trim();
        String head=dataString.substring(0,date.indexOf("-"));
        String foot=dataString.substring(date.indexOf("-")+1);
        if(dataString.length()==9){
            date=head+"-0"+foot.substring(0,foot.indexOf("-")-1);
        }else{
            date=dataString.substring(0,foot.lastIndexOf("-")-1);
        }
        return attendanceService.count(new QueryWrapper<Attendance>().eq("attendance_users",users_id).like("attendance_today",date));
    }



    /**
     * 得到所有的考勤记录信息
     * @return
     */
    @RequestMapping("/getAllAttendance")
    public List<Attendance> getAllAttendance(){
        return attendanceService.list();
    }

    /**
     * 得到某个考勤信息
     * @param attendanceId
     * @return
     */
    @RequestMapping("/getAttendanceOne")
    public Attendance getAttendanceOne(int attendanceId){
        return attendanceService.getById(attendanceId);
    }


//    public static void main(String[] args) throws ParseException {
//
//        Date now = new Date();
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date time=sdf.parse("2023-03-23 09:52:00");
//
//        long end= now.getTime();
//        long start= time.getTime();
//
//        long betweenTime=end-start;
//
//        Duration dur= Duration.between(time.toInstant() , now.toInstant());
//        System.out.println("now:"+sdf.format(now));
//        System.out.println("now:"+now.getTime());
//        System.out.println("time:"+time.getTime());
//        System.out.println("dur:"+dur.toMinutes());
//
//    }
}
